﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.Gui;

using Swaf.BizObj;
using Swaf.Container;
using Swaf.Security;

namespace FilterSort.Gui
{
	public class KeywordSetKeywordsBizProc : BaseBusinessProcess
	{
		public KeywordSetKeywordsBizProc(ActiveBizProc procState)
			:base(procState)
		{
		}

        protected override void setupRuleNames()
		{
			base.m_getBizObjListRule = "getArticleKeywordSetKeywords";
			base.m_updateBizObjRule = "updateArticleSetKeyword";
			base.m_deleteBizObjRule = "deleteArticleSetKeyword";
		}

		public override Privileges CurrentEditPrivilege
		{
			get 
			{
				Privileges priv = Privileges.ReadOnly;
				if (m_procState != null)
				{
					ActiveBizProc proc = m_procState.getParentBizProc("pid");
					if(proc != null)
						priv = proc.BizProc.CurrentEditPrivilege;
				}
				return priv;
			}
		}

		protected override object[] getBizObjListRuleParameters(object parentId, IBizObj filterSort)
		{
			return new object[] { parentId };
		}

		protected override object[] calculateParentBizObjIds()
		{
			object pid = m_procState.get("pid");
			IResults rc = App.ruleMgr.execute("createNewArticleMatchSetIfNotExist", pid, "Index");
			IBizObj articleSet = rc.CallResults as IBizObj;
			if (articleSet != null)
				return new object[] { articleSet.get("id") };
			return base.calculateParentBizObjIds();
		}

		protected override object[] getUpdateBizRuleParameters(IBizObj bizObj)
		{
			return new object[] { bizObj, m_procState.get("pid") };
		}

		public override IBizObj createNewBizObj()
		{
			IBizObj newKeyword = App.bizObjMgr.create("ArticleKeyword");
			newKeyword.put("id", new Swaf.BGuid.Guid());
			newKeyword.put("keywordSetId", calculateParentBizObjIds()[0]);
			return newKeyword;
		}

		public override IBizObj selectBizObj(object keyValue, object source)
		{
			IBizObj selected = null;
			ActiveBizProc pushCrit = m_procState.getParentBizProc("pushCriteria");
			if (pushCrit != null && source != this)
			{
				selected = base.selectBizObj(keyValue, source);
				IResults rc = App.ruleMgr.execute("getFilterSortCriteriaFromKeyword", CurrentBizObj.get("keyword"), CurrentBizObj.get("onlyCatagories"));
				pushCrit.BizProc.FilterSortCriteria = rc.CallResults as IBizObj;
				pushCrit.BizProc.clearRetrievedData();
			}
			return selected;
		}
	}
}
